package com.hg.library.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;

/**
 * Created by admin on 1817/2/23.
 */

public class SharedButton extends Button {
    private static final String TAG = "SharedButton";

    public SharedButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                start();
            }
        });
    }

    private float padding = 18;
    private float radius = 7;

    private int p;

    private boolean isStart;

    private int count;

    private float valueX, valueY;
    Paint mPaint = new Paint();

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        float p1_x = padding, p1_y = getHeight() / 2;
        float p2_x = getWidth() - padding, p2_y = padding;
        float p3_x = getWidth() - padding, p3_y = getHeight() - padding;
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.WHITE);

        canvas.drawARGB(255, 65, 72, 75);

        {
            int saveLayerId = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);

            canvas.drawCircle(p1_x, p1_y, radius, mPaint);

            canvas.drawCircle(p2_x, p2_y, radius, mPaint);

            canvas.drawCircle(p3_x, p3_y, radius, mPaint);

            canvas.restoreToCount(saveLayerId);
        }

        {
            int saveLayerId = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);

            mPaint.setStrokeWidth(4);

            if (isStart) {
                drawPL1(canvas, p);
                drawPL2(canvas, p);
            } else {
                canvas.drawLine(p1_x, p1_y, p2_x - valueX, p2_y - valueY, mPaint);

                canvas.drawLine(p1_x, p1_y, p3_x + valueX, p3_y - valueY, mPaint);
            }

            canvas.restoreToCount(saveLayerId);
        }

        if (isStart) {
            if (p2_x - valueX <= p1_x) {
                valueX = 0;
                valueY = 0;
                if (p == 1) {
                    p++;
                } else {
                    isStart = false;
                    count++;
                }
            }

            invalidate();
        }

        if (count == 1){
            start();
        }else if (count == 2){
            count = 0;
        }
    }

    private void drawPL1(Canvas canvas, int p) {

        float p1_x = padding, p1_y = getHeight() / 2;
        float p2_x = getWidth() - padding, p2_y = padding;

        float d = 2f;
        valueX += d;
        float b = (p2_x - p1_x) / (p2_y - p1_y);
        valueY += d / b;

        if (p == 1) {
            float x1 = p2_x - valueX, y1 = p2_y - valueY;

            canvas.drawLine(x1, y1, p2_x, p2_y, mPaint);//1
        } else if (p == 2) {
            canvas.drawLine(p1_x, p1_y, p2_x - valueX, p2_y - valueY, mPaint);
        }

    }

    private void drawPL2(Canvas canvas, int p) {
        float p1_x = padding, p1_y = getHeight() / 2;
        float p2_x = getWidth() - padding, p2_y = padding;
        float p3_x = getWidth() - padding, p3_y = getHeight() - padding;

        float d = 2f;
        valueX += d;
        float b = (p2_x - p1_x) / (p2_y - p1_y);
        valueY += d / b;

        if (p == 1) {

            p3_x = p1_x;
            p3_y = p1_y;

            canvas.drawLine(p1_x, p1_y, p3_x + valueX, p3_y - valueY, mPaint);
        } else if (p == 2) {

            canvas.drawLine(p1_x + valueX, p1_y - valueY, p3_x, p3_y, mPaint);
        }

    }

    public void start() {
        if (!isStart) {
            isStart = true;
            p = 1;
            invalidate();
        }
    }
}
